﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/MasterNhanSuTienLuong.Master" Inherits="System.Web.Mvc.ViewPage<DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.PhanQuyen.RolesForm>" %>

<%@ Import Namespace="Webdiyer.WebControls.Mvc" %>
<%@ Import Namespace="DTTB.DTO" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <script type="text/javascript" src="<%= ResolveUrl("~/Content/Scripts/User/PermissionsAllUsers.js") %>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/Content/Scripts/Common/DinhNghiaSoLuongSectionDongMo.js") %>"></script>
    
    <script type="text/javascript">
        $(document).ready(function () {
            selectMenu("system-SetPermissionsForRoles");
            $("#system-SetPermissionsForRoles").parent().attr("class", "active");
        });
         
    </script>

    <%Html.BeginForm(); %>
    <input type="hidden" name="CurrentPageNumber" id="CurrentPageNumber" value="<%= Model.CurrentPageNumber %>" />
    <input type="hidden" name="CurrentRolesId" id="CurrentRolesId" value="" />
    <input type="hidden" name="CurrentRolesIdTemp" id="CurrentRolesIdTemp" value="" />
    <input type="hidden" name="RolesClickFlag" id="RolesClickFlag" value="false" />
    <%--<input type="hidden" name="SearchFlag" id="SearchFlag" value="true"/>--%>
    <input type="hidden" name="UpdateFlag" id="UpdateFlag" value="false" />
    <input type="hidden" name="ResetFlag" id="ResetFlag" value="false" />
    <input type="hidden" name="NewRolesInPathsString" id="NewRolesInPathsString" />

<div class="formBox">
    <%--<div class="module">--%>
    <div class="box">
        <!-- box / title -->
		<div class="breadcrumbs">
          <ul>
            <li class="home"><a href="">Phân Quyền</a></li>
            <li><a href="">Chi tiết quyền</a></li>
            <li>Page</li>
          </ul>
        </div>
           <div class="headlines">
            <h2>
                <span>Chi tiết quyền</span>
            </h2>
               <%-- <a class="show-filter" href="#">show filter</a>--%>
        </div>

		
		<!-- end box / title -->
        <%--<div class="module_top"></div>--%>

        <%--<div class="contain_module">--%>
            <table class="table_outsite" cellspacing="0" cellpadding="0" width="100%">
                <tr>
                    <td>&nbsp;<b>Danh sách quyền:</b></td>

                    <td align="right">
                        <%=Html.DropDownList("TieuChiSearch", Model.TieuChiSearchDropDownList)%>
                        <%=Html.TextBox("GiaTriSearch", Model.GiaTriSearch, new { @onkeypress = "if (event.keyCode==13) { return DoSearch(); }", Style = "width:210px" })%>
                        <img onclick="javascript:DoSearch();" style="vertical-align: middle; cursor:pointer" src="<%=ResolveUrl("~/Content/ImagesAdmin/search_icon.gif")%>"
                            alt="Tìm quyền" title="Tìm quyền"/>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <table cellspacing="0" cellpadding="0" class="tab tab-drag">
                            <tr class="top nodrop nodrag" style="font-weight: bold">
                                <td class="first" width="10%" style="text-align:center">
                                    &nbsp;<strong>STT</strong>
                                </td>
                                <td nowrap="nowrap" >
                                    &nbsp;<strong>Tên Role</strong>
                                </td>
                                <td nowrap="nowrap" align="center">
                                    <strong >Đang sử dụng</strong>
                                </td>
                                <td nowrap="nowrap" align="center">
                                    <strong>Permission</strong>
                                </td>
                                <td nowrap="nowrap">
                                    &nbsp;<strong>Ngày cập nhật</strong>
                                </td>
                                <td width="14%" class="last" nowrap="nowrap">
                                    &nbsp;<strong>Người cập nhật</strong>
                                </td>
                            </tr>
                            <%--Display Roles List--%>
                            <%
                                IList<RolesDto> dtos = Model.RolesPagedList;
                                if (dtos != null && dtos.Count > 0)
                                {%>
                            <%
                                int dem = 0;
                                foreach (RolesDto dto in dtos)
                                {%>
                            <%if (dto.ID.ToString() == Model.CurrentRolesId)
                              {
                            %>
                            <tr class="item_row" onclick="javascript:highlight(this);onClickRoles(<%=dto.ID%>);" style="background-color: #f1f0ed">
                                <td class="first" style="text-align:center">
                                    &nbsp;<%=++dem %>
                                </td>
                                <td>
                                    &nbsp;<%=dto.LoweredRoleName %>
                                </td>
                                <td style="text-align:center">
                                    <%if (dto.IsUsed)
                                      {%>
                                    Yes
                                    <%}
                                      else
                                      {%>
                                    No
                                    <%}%>
                                </td>
                                <td style="text-align:center">
                                    <%if (dto.HavePermission)
                                      {%>
                                    Yes
                                    <%}
                                      else
                                      {%>
                                    No
                                    <%}%>
                                </td>
                                <td nowrap="nowrap">
                                    &nbsp;<%=dto.UpdateDate %>
                                </td>
                                <td class="last" nowrap="nowrap">
                                    &nbsp;<%=dto.UpdateByUser %>
                                </td>
                            </tr>
                            <%
                                }
                              else
                              {
                            %>
                            <tr class="item_row" onclick="javascript:onClickRoles(<%=dto.ID%>);">
                                <td class="first" style="text-align:center">
                                    &nbsp;<%=++dem %>
                                </td>
                                <td>
                                    &nbsp;<%=dto.LoweredRoleName %>
                                </td>
                                <td style="text-align:center">
                                    <%if (dto.IsUsed)
                                      {%>
                                    Yes
                                    <%}
                                      else
                                      {%>
                                    No
                                    <%}%>
                                </td>
                                <td style="text-align:center">
                                    <%if (dto.HavePermission)
                                      {%>
                                    Yes
                                    <%}
                                      else
                                      {%>
                                    No
                                    <%}%>
                                </td>
                                <td nowrap="nowrap">
                                    &nbsp;<%=dto.UpdateDate %>
                                </td>
                                <td class="last" nowrap="nowrap">
                                    &nbsp;<%=dto.UpdateByUser %>
                                </td>
                            </tr>
                            <%
                                } %>
                            <%}%>
                            <%}%>
                            <%--End Display--%>
                            <tr class="border_bottom">
                                <td class="first" style="background: #DDDDDD">
                                </td>
                                <td class="last" colspan="6" align="right" style="background: #DDDDDD; padding-right: 10px">
                                    <div>
                                        <%=Html.Pager(Model.RolesPagedList,new PagerOptions{PageIndexParameterName="id"}) %></div>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
            <div class="pagebreak">&nbsp;</div>
            

        <table class="table_outsite" width="100%" >
            <tr>
                <td>
                    <table cellspacing="0" cellpadding="0" class="table_module"style="margin-bottom: 20px;" >
                        <tr>
                            <td class="first" id="NoteMessage1" align="left" colspan="3">
                                <strong>
                                    <%if (Model.UpdateSatusMessage == "Cập nhật thành công !")
                                      {%>
                                    <div style="color: Blue"><%=Model.UpdateSatusMessage%></div>
                                    <%}
                                      else if (Model.UpdateSatusMessage == "Cập nhật thất bại, xin thử lại !")
                                      {%>
                                    <div style="color: Red"><%=Model.UpdateSatusMessage %></div>
                                    <%}%>
                                </strong>
                            </td>
                            <td align="right" class="btn-submit">
                                <input type="button" class="button" value="Cập nhật" onclick="onUpdate();" />
                                <input type="reset" class="button" value="Hủy thay đổi" />
                            </td>
                        </tr>
                    </table>
                    
                    <%--Chia hệ thống phân quyền thành từng block một--%>
                    <%for (int d = 0; d < Model.Blocks.Count; d++ )
                    {%>
                
                    <p style="margin:0px 0px 10px 40px"><b>Phân quyền : <%=Model.Blocks[d].ToUpper()%> &nbsp;</b></p>
                    <div id="arrow<%=d + 1 %>" class="background_arrow_up arrow" style="float: left; cursor: pointer;
                        clear: both; margin-top: -32px; margin-right:0px">
                    </div>
                    <div id="open_close<%=d + 1 %>" class="bo" style="width: 100%; border: 1px; margin-left: 0%; margin-bottom: 15px;">
                        <table border="1" cellspacing="0" cellpadding="0" class="tab tab-drag" style=" border-collapse:collapse">
                            <tr  class="top nodrop nodrag" style="font-weight: bold">
                                <td class="first">
                                    <b>Chức năng</b>
                                </td>
                                <td width="21%" align="center"  nowrap="nowrap">
                                    &nbsp;<b>Create/Edit/Update</b>
                                </td>
                                <td width="13%" align="center"  nowrap="nowrap">
                                    &nbsp;<b>View/List</b>
                                </td>
                                <td width="11%" align="center"  nowrap="nowrap">
                                    &nbsp;<b>Delete</b>
                                </td>
                                <td width="11%" align="center" class="last" nowrap="nowrap">
                                    &nbsp;<b>Execute</b>
                                </td>
                            </tr>      
                            <%for (int i = 0; i < Model.ModulesByBlockArray[d].Count; i++)
                              {%>
                            <tr onclick="highlight(this); doClickCheckbox();" class="item_row">                        
                                <td>
                                    &nbsp;<%=Model.ModulesByBlockArray[d][i]%>
                                </td>
                                <%--Case1 : Permission : Create/Edit/Update | Build CheckBox--%>
                                <%
                                    bool checkBoxFlag1 = false;
                                    for (int j = 0; j < Model.PathsList.Count; j++)
                                    {
                                        if (Model.PathsForRolesList != null)
                                        {
                                            if ((Model.PathsList[j].Modules == Model.ModulesByBlockArray[d][i]) && (Model.PathsList[j].PermissionType == "Create_Edit_Update"))
                                            {
                                                checkBoxFlag1 = true;
                                                bool checkedFlag = false;
                                                for (int k = 0; k < Model.PathsForRolesList.Count; k++)
                                                {
                                                    if ((Model.PathsForRolesList[k].Modules == Model.ModulesByBlockArray[d][i]) && (Model.PathsForRolesList[k].PermissionType == "Create_Edit_Update"))
                                                    {%>
                                <td align="center">
                                    <input id="<%=Model.ModulesByBlockArray[d][i] + "-" + Model.PathsForRolesList[k].PermissionType%>"
                                        type="checkbox" value="<%=Model.PathsForRolesList[k].ID %>" checked="checked"
                                        onclick="unSelectCreate('<%=Model.ModulesByBlockArray[d][i]%>','<%=Model.PathsForRolesList[k].PermissionType %>');" />&nbsp;
                                </td>
                                <%
                                    checkedFlag = true;
                                    break;
                                                }
                                            }

                                            if (checkedFlag == false)
                                            {%>
                                <td align="center">
                                    <input id="<%=Model.ModulesByBlockArray[d][i]+ "-Create_Edit_Update"%>" type="checkbox" value="<%=Model.PathsList[j].ID %>"
                                        onclick="unSelectCreate('<%=Model.ModulesByBlockArray[d][i]%>','-Create_Edit_Update');" />&nbsp;
                                </td>
                                <%}
                                            break;
                                        }
                                    }
                                }
                                if (checkBoxFlag1 == false)
                                {
                                %>
                                <td align="center">
                                    &nbsp;
                                </td>
                                <%
                                    }
                        
                                %>
                                <%--Case2 : Permission : View/List | Build CheckBox--%>
                                <%
                                    bool checkBoxFlag2 = false;
                                    for (int j1 = 0; j1 < Model.PathsList.Count; j1++)
                                    {
                                        if ((Model.PathsList[j1].Modules == Model.ModulesByBlockArray[d][i]) && (Model.PathsList[j1].PermissionType == "View_List"))
                                        {
                                            checkBoxFlag2 = true;
                                            bool checkedFlag = false;
                                            for (int k1 = 0; k1 < Model.PathsForRolesList.Count; k1++)
                                            {
                                                if ((Model.PathsForRolesList[k1].Modules == Model.ModulesByBlockArray[d][i]) && (Model.PathsForRolesList[k1].PermissionType == "View_List"))
                                                {%>
                                <td align="center">
                                    <input id="<%=Model.ModulesByBlockArray[d][i] + "-" + Model.PathsForRolesList[k1].PermissionType%>"
                                        value="<%=Model.PathsForRolesList[k1].ID %>" type="checkbox" checked="checked"
                                        onclick="unSelectView('<%=Model.ModulesByBlockArray[d][i] %>','<%=Model.PathsForRolesList[k1].PermissionType %>');" />&nbsp;
                                </td>
                                <%
                                    checkedFlag = true;
                                    break;
                                            }
                                        }

                                        if (checkedFlag == false)
                                        {%>
                                <td align="center">
                                    <input id="<%=Model.ModulesByBlockArray[d][i]+ "-View_List"%>" value="<%=Model.PathsList[j1].ID %>"
                                        type="checkbox" onclick="unSelectView('<%=Model.ModulesByBlockArray[d][i] %>','-View_List');" />&nbsp;
                                </td>
                                <%}
                                        break;
                                    }
                                }
                                if (checkBoxFlag2 == false)
                                {
                                %>
                                <td align="center">
                                    &nbsp;
                                </td>
                                <%
                                    }    
                                %>
                                <%--Case3 : Permission : Delete | Build CheckBox--%>
                                <%
                                    bool checkBoxFlag3 = false;
                                    for (int j2 = 0; j2 < Model.PathsList.Count; j2++)
                                    {
                                        if ((Model.PathsList[j2].Modules == Model.ModulesByBlockArray[d][i]) && (Model.PathsList[j2].PermissionType == "Delete"))
                                        {
                                            checkBoxFlag3 = true;
                                            bool checkedFlag = false;
                                            for (int k2 = 0; k2 < Model.PathsForRolesList.Count; k2++)
                                            {
                                                if ((Model.PathsForRolesList[k2].Modules == Model.ModulesByBlockArray[d][i]) && (Model.PathsForRolesList[k2].PermissionType == "Delete"))
                                                {%>
                                <td align="center">
                                    <input id="<%=Model.ModulesByBlockArray[d][i] + "-" + Model.PathsForRolesList[k2].PermissionType %>"
                                        type="checkbox" value="<%=Model.PathsForRolesList[k2].ID %>" checked="checked"
                                        onclick="unSelectDelete('<%=Model.ModulesByBlockArray[d][i] %>','<%=Model.PathsForRolesList[k2].PermissionType %>');" />&nbsp;
                                </td>
                                <%
                                    checkedFlag = true;
                                    break;
                                            }
                                        }

                                        if (checkedFlag == false)
                                        {%>
                                <td align="center">
                                    <input id="<%=Model.ModulesByBlockArray[d][i] + "-Delete"%>" type="checkbox" value="<%=Model.PathsList[j2].ID %>"
                                        onclick="unSelectDelete('<%=Model.ModulesByBlockArray[d][i] %>','-Delete');" />&nbsp;
                                </td>
                                <%}
                                        break;
                                    }
                                }
                                if (checkBoxFlag3 == false)
                                {
                                %>
                                <td align="center">
                                    &nbsp;
                                </td>
                                <%
                                    }       
                                %>
                        
                                <%--Case4 : Permission : Execute | Build CheckBox--%>
                                        <%
                                            bool checkBoxFlag4 = false;
                                            for (int j3 = 0; j3 < Model.PathsList.Count; j3++)
                                            {
                                                if ((Model.PathsList[j3].Modules == Model.ModulesByBlockArray[d][i]) && (Model.PathsList[j3].PermissionType == "Execute"))
                                                {
                                                    checkBoxFlag4 = true;
                                                    bool checkedFlag = false;
                                                    for (int k = 0; k < Model.PathsForRolesList.Count; k++)
                                                    {
                                                        if ((Model.PathsForRolesList[k].Modules == Model.ModulesByBlockArray[d][i]) && (Model.PathsForRolesList[k].PermissionType == "Execute"))
                                                        {%>
                                        <td align="center">
                                            <input id="<%=Model.ModulesByBlockArray[d][i] + "-" + Model.PathsForRolesList[k].PermissionType %>"
                                                type="checkbox" value="<%=Model.PathsForRolesList[k].ID %>" checked="checked"/>&nbsp;
                                        </td>
                                        <%
                                            checkedFlag = true;
                                            break;
                                                        }
                                                    }

                                                    if (checkedFlag == false)
                                                    {%>
                                        <td align="center">
                                            <input id="<%=Model.ModulesByBlockArray[d][i] + "-Execute"%>" type="checkbox" value="<%=Model.PathsList[j3].ID %>"
                                                onclick="unSelectDelete('<%=Model.ModulesByBlockArray[d][i] %>','-Delete');" />&nbsp;
                                        </td>
                                        <%}
                                                        break;
                                                }
                                            }
                                            if (checkBoxFlag4 == false)
                                            {
                                        %>
                                        <td align="center">
                                            &nbsp;
                                        </td>
                                        <%
                                            }
                                        %>
                            </tr>
                            <%
                                }%>
                        </table>
                    </div>
                    <br/>
                    
                    <%
                    }//end Vòng For cho Block
                    %>

                    <table cellspacing="0" cellpadding="0" class="table_module" width="100%">
                        <tr>
                            <td class="first" id="NoteMessage2" align="left" colspan="3">
                                <strong>
                                    <%if (Model.UpdateSatusMessage == "Cập nhật thành công !")
                                      {%>
                                    <div style="color: Blue"><%=Model.UpdateSatusMessage%></div>
                                    <%}
                                      else if (Model.UpdateSatusMessage == "Cập nhật thất bại, xin thử lại !")
                                      {%><div style="color: Red"><%=Model.UpdateSatusMessage %></div>
                                    <%}%>
                                </strong>
                            </td>
                            <td align="right" class="btn-submit">                        
                                <input type="button" class="button" value="Cập nhật" onclick="onUpdate();" />
                                <input type="reset" class="button" value="Hủy thay đổi" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
        <%--</div>--%>
    </div>
</div>
    <script language="javascript" type="text/javascript">

        // Search ..............................

        function DoSearch() {
            var search = $("#TieuChiSearch").val();
            var value = $("#GiaTriSearch").val();

            document.forms[0].action = basePath + "Roles/ManageRolesWithPermissions?TimKiem=" + search + "&GiaTri=" + value;
            document.forms[0].method = "post";
            document.forms[0].submit();
        }



        function doPaging(pageNumber) {
            document.getElementById("PageNumber").value = pageNumber;
            document.getElementById("SearchOrPagingClickFlag").value = true;
            document.forms[0].action = '<%=ResolveUrl("~/Roles/ManageRolesWithPermissionsUpdate/")%>';
            document.forms[0].method = "post";
            document.forms[0].submit();
        }

        function onSearch() {
            document.getElementById("SearchOrPagingClickFlag").value = true;
            document.forms[0].action = '<%=ResolveUrl("~/Roles/ManageRolesWithPermissionsUpdate/")%>';
            document.forms[0].method = "post";
            document.forms[0].submit();
        }

        function onClickRoles(currentRolesId) {
            document.getElementById("CurrentPageNumber").value = '<%=Model.CurrentPageNumber%>';
            document.getElementById("RolesClickFlag").value = true;
            document.getElementById("CurrentRolesId").value = currentRolesId;
            document.getElementById("CurrentRolesIdTemp").value = currentRolesId;
            document.forms[0].action = '<%=ResolveUrl("~/Roles/ManageRolesWithPermissionsUpdate")%>';
            document.forms[0].method = "post";
            document.forms[0].submit();
        }

        function onReset() {
            document.getElementById("CurrentRolesId").value = '<%=Model.CurrentRolesId %>';
            document.getElementById("CurrentRolesIdTemp").value = '<%=Model.CurrentRolesId %>';
            document.getElementById("ResetFlag").value = true;
            document.forms[0].action = '<%=ResolveUrl("~/Roles/ManageRolesWithPermissionsUpdate/")%>';
            document.forms[0].method = "post";
            document.forms[0].submit();
        }

        function onUpdate() {
            var currentRoleId = '<%=Model.CurrentRolesId %>';
            if (currentRoleId == "0" || currentRoleId == "null" || currentRoleId == "") {
                alert("No Role for assigning Permission !");
                return;
            }
            

            for (var i = 0; i < document.forms[0].length; i++) {
                var e = document.forms[0].elements[i];
                if (e.type == 'checkbox' && e.checked) {
                    document.getElementById("NewRolesInPathsString").value = document.getElementById("NewRolesInPathsString").value + "|" + e.id;
                }
            }
            document.getElementById("CurrentPageNumber").value = '<%=Model.CurrentPageNumber%>';
            document.getElementById("CurrentRolesId").value = '<%=Model.CurrentRolesId %>';
            document.getElementById("CurrentRolesIdTemp").value = '<%=Model.CurrentRolesId %>';
            document.getElementById("UpdateFlag").value = true;
            document.forms[0].action = '<%=ResolveUrl("~/Roles/ManageRolesWithPermissionsUpdate/")%>';
            document.forms[0].method = "Post";
            document.forms[0].submit();
        }

        function doSearchByKey(elem) {
            if (elem.keyCode == 13) {
                document.forms[0].action = basePath + "Roles/ManageRolesWithPermissions/";
                document.forms[0].method = "post";
                document.forms[0].submit();
            }
        }

        function doClickCheckbox() {
            document.getElementById('NoteMessage1').innerHTML = "";
            document.getElementById('NoteMessage2').innerHTML = "";
        }
    
    </script>

    <%Html.EndForm(); %>
</asp:Content>
